//
//  XRmenuFileManage.m
//  XRmenu
//
//  Created by Gao Yan on 11-11-23.
//  Copyright 2011年 foxhis. All rights reserved.
//

#import "XRmenuFileManage.h"


@implementation XRmenuFileManage
#pragma mark 指定文件获取方法
// 返回OEM厂商图片
+ (NSString *) getOEMImageFile:(XRmenuError**)outError
{
	// todo 依据数据取指定目录  还需增加一个开关控制是否使用名称贴图
	NSString *retResault = [[self documentPath] stringByAppendingPathComponent:@"oem.png"];
    DebugLog(@"retResult=%@", retResault);
	if (![self fileExistByPath:retResault]) {
		retResault = [[self nativeDocumentPath] stringByAppendingPathComponent:@"oem.png"];
        DebugLog(@"retResult=%@", retResault);
		if (![self fileExistByPath:retResault]) {
			if (outError) {
				*outError = [XRmenuError createError:@"无法获取OEM厂商图片资料" ECode:-21001];
			}
		}
	}	
	return retResault;	
}

// 返回系统配置文件plist
+ (NSString *) getViewConfigFile
{
	NSString *retResault = [[self documentPath] stringByAppendingPathComponent:@"XRViewConfig.plist"];
	if ([[NSFileManager defaultManager] fileExistsAtPath:retResault]) {
		return retResault;
	}else{
		return [[self nativeDocumentPath] stringByAppendingPathComponent:@"XRViewConfig.plist"];
	}	
}
// 准备翻译用字典
+ (NSString *) getTranslate
{
	NSString *retResault = [[self documentPath] stringByAppendingPathComponent:@"XRTranslate.plist"];
	if ([[NSFileManager defaultManager] fileExistsAtPath:retResault]) {
		return retResault;
	}else{
		return [[self nativeDocumentPath] stringByAppendingPathComponent:@"XRTranslate.plist"];
	}	
}

#pragma mark 目录参数操作
// 返回程序安装后Ipad设备的沙盒目录
+ (NSString *)documentPath{
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	return [paths objectAtIndex:0];
}
// 返回程序打包中的资源目录
+ (NSString *)nativeDocumentPath{
	return [[XRmenuFileManage boundlePath] stringByAppendingPathComponent:@"/"];
}
// 返回程序打包的资源包
+ (NSString *)boundlePath {
	return [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"XRmenuResource.bundle"];
}

#pragma mark 持久化保存
// 返回系统持久化保存状态的文件
+ (NSString *)savedataFile {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"data_%@.plist", kCurrentDbNameSurfix]];
}

+ (NSString *)savedataFile:(NSString*)plistname {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
	NSString *ret = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"data_%@_%@.plist", plistname, kCurrentDbNameSurfix]];
	if ([self fileExistByPath:ret]) {
		return ret;
	}else {
		return nil;
	}
}

+ (void) checkSaved
{
	NSString *filePath = [self savedataFile];
	if ([self fileExistByPath:filePath]) {
		NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
		if ([array count] == 3) {
			// 保存的状态数正确，nothing todo
		}else {
			// 保存的状态数不一致，可能是升级原因，需要重新初始化
			[self fileDelete:filePath];
			[self initSaved:YES];
		}
	}else {
		[self initSaved:YES];
	}
}

+ (void) initSaved:(BOOL)isnew
{
	NSMutableArray *array = [[NSMutableArray alloc] init];
	if (isnew) {
		[array addObject:@"zh-Hans"];//en
		[array addObject:@"default skin"];
		[array addObject:[DEVICEUDID stringByReplacingOccurrencesOfString:@"-" withString:@""]];
	}else {
		[array addObject:APPSESSION.language];
		[array addObject:APPSESSION.uistyle];
		[array addObject:[DEVICEUDID stringByReplacingOccurrencesOfString:@"-" withString:@""]];
	}
	[array writeToFile:[self savedataFile] atomically:YES];
	[array release];		
	
	//保存搜索历史记录
//	[APPSESSION.filteredListContent writeToFile:[self savedataFile:@"search_history"] atomically:YES];
}

#pragma mark 文件基本操作
+ (NSArray *)allFilesInFolder:(NSString *)folderName{
	DebugLog(@"Folder Path:%@",folderName);
	return [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderName error:nil];
}

+ (NSArray *)allContentsInPath:(NSString *)directoryPath{
	return [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:nil];
}

+ (NSArray *)allFilesInPathAndItsSubpaths:(NSString *)directoryPath{
	NSMutableArray *allContentsPathArray = [[[NSFileManager defaultManager] subpathsAtPath:directoryPath] mutableCopy];
	BOOL isDir = NO;
	for (int i = [allContentsPathArray count] - 1;i>=0;i--) {
		NSString *path = [allContentsPathArray objectAtIndex:i];
		if (![[NSFileManager defaultManager] fileExistsAtPath:[directoryPath stringByAppendingPathComponent:path] isDirectory:&isDir] || isDir) {
			[allContentsPathArray removeObject:path];
		}
	}
	return [allContentsPathArray autorelease];
}

+ (BOOL)fileExistByPath:(NSString *)storePath{
	BOOL rtn = NO;
	rtn = [[NSFileManager defaultManager] fileExistsAtPath:storePath];
	return rtn;
}

+ (BOOL)fileAttributes:(NSString *)fileName targetSize:(NSInteger)fileSize
{
	NSFileManager *fileManager = [NSFileManager defaultManager];
	NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:fileName error:nil];
	if (fileAttributes != nil) {
		NSNumber *size = [fileAttributes objectForKey:NSFileSize];
		if ([size intValue] == fileSize) {
			return YES;
		}else {
			return NO;
		}
	}
	else {
		return NO;
	}
}

+ (BOOL)fileDelete:(NSString *)sName {
	NSError *error=nil;
	if ([self fileExistByPath:sName]) {
		NSFileManager *fileManager = [NSFileManager defaultManager];
		[fileManager removeItemAtPath:sName error:&error];
	}
	if (error == nil) {
		return YES;
	}else {
		return NO;
	}
}

+ (BOOL)filesCopy:(NSString *)sName TargetName:(NSString *)tName{
	NSFileManager *fileManaget = [NSFileManager defaultManager];
	NSError *error=nil;
	return [fileManaget copyItemAtPath:sName toPath:tName error:&error];
}

@end
